package org.recruit.system.controller;

import java.util.List;

import org.recruit.system.domain.Article;
import org.recruit.system.service.IArticleService;
import org.recruit.system.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 文章Controller
 *
 * @author teapot
 * @date 2020-11-12
 */
@Controller
@RequestMapping("/system/article")
public class ArticleController {
    private String prefix = "system/article";

    @Autowired
    private IArticleService articleService;

    @GetMapping()
    public String article() {
        return prefix + "/article";
    }

    /**
     * 查询文章列表
     */
    @PostMapping("/list")
    @ResponseBody
    public AjaxResult list(Article article) {
        //startPage();
        List<Article> list = articleService.selectArticleList(article);
        return AjaxResult.success(list);
    }


    /**
     * 新增文章
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存文章
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Article article) {
        return AjaxResult.success(articleService.insertArticle(article));
    }

    /**
     * 修改文章
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Article article = articleService.selectArticleById(id);
        mmap.put("article", article);
        return prefix + "/edit";
    }

    /**
     * 修改保存文章
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Article article) {
        return AjaxResult.success(articleService.updateArticle(article));
    }

    /**
     * 删除文章
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return AjaxResult.success(articleService.deleteArticleByIds(ids));
    }
}
